package com.service.sys;

import com.common.ErrorCodeEnum;
import com.model.base.BaseEntity;
import com.common.emums.DataStatus;
import com.common.exception.BusinessException;
import com.common.utils.StringUtils;
import com.core.springSecurity.common.SecurityUtils;
import com.dao.sys.SysDeptMapper;
import com.github.pagehelper.PageHelper;
import com.model.pojo.sys.SysDept;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: zql
 * @Description: 部门服务
 * @Data: 16:48 2021/5/11
 */
@Service
public class SysDeptService {
    private static final String DEPT_DELIMETER = ",";

    @Resource
    SysDeptMapper sysDeptMapper;

    /**
     * @Author: zql
     * @Description: 创建部门
     * @param sysDept 部门
     * @Data: 11:36 2021/5/12
     */
    public void create(SysDept sysDept)
    {
        if( sysDept.getParentId()!= 0) {
            String ancestors =getAncestors(sysDept.getParentId());
            sysDept.setAncestors(ancestors + DEPT_DELIMETER + sysDept.getParentId());
        }
        insert(sysDept);
    }

    /**
     * @Author: zql
     * @Description: 递归查询父级部门状态并返回父级部门的祖级列表
     * @param parentId 父级部门id
     * @Data: 15:27 2021/7/2
     */
    private String getAncestors(Long parentId) {
        SysDept parentDept = sysDeptMapper.selectByPrimaryKey(parentId,DataStatus.OK.getCode());
        if (StringUtils.isNull(parentDept)) {
            throw new BusinessException(ErrorCodeEnum.PARENT_DEPT_NOT_FOUND);
        }
        // 如果父节点不为正常状态,则不允许新增子节点
        if (parentDept.getParentId() != 0) {
            getAncestors(parentDept.getParentId());
        }
        return parentDept.getAncestors();
    }

    /**
     * @Author: zql
     * @Description: 查看部门列表
     * @param sysDeptId 部门id
     * @param baseEntity 基类查询
     * @Data: 14:10 2021/5/12
     */
    public List<SysDept> getDeptList(Long sysDeptId, BaseEntity baseEntity){
        PageHelper.clearPage();
        PageHelper.startPage(baseEntity.getPage(), baseEntity.getPageSize(),baseEntity.getSortRow()+baseEntity.getSortOrder());
        return sysDeptMapper.selectList(sysDeptId,baseEntity.getIsDelete());
    }

    public SysDept getByName(String deptName,Byte isDelete){
        SysDept sysDept=  sysDeptMapper.selectByDeptName(deptName,isDelete);
        return sysDept;
    }

    public SysDept get(Long sysDeptId,Byte isDelete){
        SysDept sysDept= sysDeptMapper.selectByPrimaryKey(sysDeptId,isDelete);
        if(StringUtils.isNull(sysDept)){
            throw new BusinessException(ErrorCodeEnum.DEPT_NOT_FOUND);
        }
        return sysDept;
    }

    public SysDept get(Long sysDeptId){
        SysDept sysDept= sysDeptMapper.selectByPrimaryKey(sysDeptId,null);
        return sysDept;
    }

    private void insert(SysDept dept){
        String name=SecurityUtils.getUsername();
        dept.setCreateTime(null);
        dept.setUpdateTime(null);
        dept.setCreator(name);
        dept.setModifier(name);
        sysDeptMapper.insertSelective(dept);
    }

    public void update(SysDept dept){
        dept.setCreator(null);
        dept.setCreateTime(null);
        dept.setUpdateTime(null);
        dept.setModifier(SecurityUtils.getUsername());
        sysDeptMapper.updateByPrimaryKeySelective(dept);
    }

    public boolean has(Long sysDeptId){
        return sysDeptMapper.has(sysDeptId, DataStatus.OK.getCode()) > 0;
    }

    public boolean has(Long sysDeptId,Byte isDelete){
        return sysDeptMapper.has(sysDeptId, isDelete) > 0;
    }

    public boolean hasChild(Long sysDeptId,Byte isDelete){
        return sysDeptMapper.hasChild(sysDeptId, isDelete) > 0;
    }

    public boolean hasName(String name,Byte isDelete){
        return sysDeptMapper.hasName(name, isDelete) > 0;
    }
}
